﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using HYCodeBase.Math;
using HYRevitCode.CurveUtility;

namespace HYRevitCode.ShortPathSuite
{
	public class ShortPahUtilityMethod
	{
		public static bool TryCalCulateMidVerticalPoints(XYZ inputPointOne, XYZ inputPointTwo, List<Curve> inputBoundary, out List<XYZ> lstAppendPoints, bool ifOnlyFastest = true)
		{
			lstAppendPoints = new List<XYZ>();
			if (inputPointOne == null || inputPointTwo == null || inputPointOne.IsAlmostEqualTo(inputPointTwo))
			{
				return false;
			}
			Line line;
			try
			{
				line = Line.CreateBound(inputPointOne, inputPointTwo);
			}
			catch (ArgumentsInconsistentException)
			{
				return false;
			}
			XYZ xyz = line.Evaluate(0.5, true);
			XYZ xyz2 = new XYZ(line.Direction.Y, -line.Direction.X, line.Direction.Z);
			Line line2 = Line.CreateUnbound(xyz, xyz2);
			double parameter = line2.Project(xyz).Parameter;
			XYZ xyz3 = null;
			double val = double.MaxValue;
			XYZ xyz4 = null;
			double val2 = double.MinValue;
			using (List<Curve>.Enumerator enumerator = inputBoundary.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					XYZ xyz5;
					if (CurveUtilityMethod.TryGetIntersectionPoint(enumerator.Current, line2, out xyz5))
					{
						if (!ifOnlyFastest)
						{
							lstAppendPoints.Add(xyz5);
						}
						else
						{
							double parameter2 = line2.Project(xyz5).Parameter;
							if (!MathUtility.Lessthan_Or_Equal(parameter2, parameter) && MathUtility.LessThan(parameter2, val))
							{
								val = parameter2;
								xyz3 = xyz5;
							}
							else if (MathUtility.LessThan(parameter2, parameter) && !MathUtility.Lessthan_Or_Equal(parameter2, val2))
							{
								val2 = parameter2;
								xyz4 = xyz5;
							}
						}
					}
				}
			}
			if (xyz3 != null)
			{
				lstAppendPoints.Add(xyz3);
			}
			if (xyz4 != null)
			{
				lstAppendPoints.Add(xyz4);
			}
			return lstAppendPoints.Count != 0;
		}
	}
}
